// packagePay/pages/new-resident-detail/new-resident-detail.js
import { getProject } from "../../../api/app"
import { getWorkerListGroup ,getProjectLeaderList, updatePayEnterpriseInfo, getProjectDetailInfo } from "../../api/pay"
import user from "../../../utils/user"
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        projectId:'',
        projectInfo:{},
        address:{}, //打卡地点集合
        describes:'',// 打卡地点
        ageArr:[],
        workerList: [], //工人列表
        leaderList: [], //管理员列表
        signInMode: '',
        isDeviceClock: '',
        hardwareId:[],
        ibeaconIds:[], //硬件打卡id集合
        timeList: [], // 自定义时间选择
        noLimit:true,
        noLimitGirl:false,
        noLimitMan:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {

        console.log(options,'options')
        const ageArr = [['不限制'],['不限制']]
        const ageGirlArr = [['不限制'],['不限制']]
        for(let i=14;i<=80;i++){
            ageArr[0].push(i)
            ageGirlArr[0].push(i)
        }
        ageArr[1] = ageArr[0]
        ageGirlArr[1] = ageGirlArr[0]

        const timeList = [['全天'],[]]
        for(let i=14;i<=80;i++){
            ageArr[0].push(i)
        } 
        ageArr[1] = ageArr[0]
        for(let i = 0 ; i<24 ; i++ ){
            timeList[0].push(i<10 ? '0' + i : i)
        }
        for(let k = 0; k<60 ;k++ ){
            timeList[1].push(k<10 ? '0' + k : k)
        }
        console.log(timeList,'timeList')
        this.setData({
            ageArr:ageArr,
            ageGirlArr,
            projectId:options.id,
            timeList
        })
        user.waitLogin().then(res=>{
            this.getProjectInfo()
        })
    },
    onReady: function () {

    },
    onShow () {
        user.waitLogin().then(res=>{
        this.getWorkerListGroup()
        this.getProjectLeaderList()
    })
    },
    // 获取岗位详情信息
    getProjectInfo(){
        getProjectDetailInfo({
            id: this.data.projectId
        }).then(res=>{
            // .replace(/~/g,'不限制')
            console.log(res,'岗位信息是啥11')
            const projectInfo = res
            projectInfo.ageRangeMan =  res.ageRange? (res.ageRange == '~-~'? '不限制-不限制' :res.ageRange.replace(/~/g,'不限制')) : '不限制-不限制'
            // projectInfo.ageRangeMan =  res.ageRange ?  res.ageRange.replace(/~/g,'不限制') : res.ageRange == '~-~' ? res.ageRange.replace(/~-~/g,'不限制-不限制') :'不限制-不限制'
            projectInfo.ageRangeWomen = res.ageRangeFemale ? (res.ageRangeFemale == '~-~'? '不限制-不限制' :res.ageRangeFemale.replace(/~/g,'不限制'))  : '不限制-不限制'
            if(res.ageRangeMan == '不限制-不限制' && res.ageRangeWomen == '不限制-不限制'){
                console.log('进来了吗')
                projectInfo.ageRange = '不限制-不限制'
                this.setData({
                    noLimit:true
                })
            }else{
                this.setData({
                    noLimit:false
                })
            }
            this.setData({
                projectInfo,
                describes: projectInfo.signPlace,
                "address.longitude":projectInfo.signLocation ? projectInfo.signLocation.split(',')[0] : '',
                "address.latitude":projectInfo.signLocation ? projectInfo.signLocation.split(',')[1] : '',
                "address.radius":projectInfo.signRange,
                isDeviceClock:projectInfo.ibeaconIds ? (projectInfo.ibeaconIds.length ? '已开启硬件打卡' : '') : '',
                ibeaconIds: projectInfo.ibeaconIds,
                signInMode:projectInfo.signInMode
            })
            console.log(this.data.projectInfo,'res')
        })
    },
    // 前往选择打卡地点
    onSelectAddress(e) {
        console.log(e.currentTarget.dataset.length);
        let length = e.currentTarget.dataset.length
        wx.navigateTo({
            url: '/packagePay/pages/clock-address/clock-address?len=' + length
        })
    },
    // 接受clock-address的调用
    selectAddressComplete(data) {
        console.log(data, '打卡地点返回')
        this.setData({
            address: data,
            describes:data.current.name
        })
    },
    // 切换白班夜班    
    onChangeClockMode(event){
        console.log(event,'event')
        this.setData({
           'projectInfo.dayNightFlag':event.detail.value
        })
    },
    // 点击选择上班时间
    onSignTimeStart(event){
        console.log(event,'event')
        const { value } = event.detail
        const { timeList } = this.data
        if(value[0] == 0){
            this.setData({
                'projectInfo.signTimeStart': '全天'
            })
            return
        }
        this.setData({
            'projectInfo.signTimeStart':timeList[0][value[0]] + ":" + timeList[1][value[1]]
        })
    },
    // 点击选择下班时间
    onSignTimeEnd(event){
        const { value } = event.detail
        const { timeList } = this.data
        if(value[0] == 0){
            this.setData({
                'projectInfo.signTimeEnd': '全天'
            })
            return
        }
        this.setData({
            'projectInfo.signTimeEnd':timeList[0][value[0]] + ":" + timeList[1][value[1]]
        })
    },
    // 修改员工押薪
    onStaffUnderpay (event) {
        console.log(event, 'event')
        const { value } = event.detail
        this.setData({
            'projectInfo.staffUnderpay': value
        })
        console.log(this.data.projectInfo.staffUnderpay, 'staffUnderpay')
    },
    onChoiceAge(event){
        const { value } = event.detail
        console.log(event,'event')
        if(value[0] !== 0 && value[1] !== 0){
            if( value[0] > value[1] ){
                app.toast('男性起始年龄不可大于结束年龄')
                return
            }
            if( value[1] < value[0] ){
                app.toast('男性结束年龄不可小于开始年龄')
                return
            }
        }
        const { ageArr } = this.data
        if(event.detail.value[0] == 0 && event.detail.value[1] == 0){
            this.setData({
                noLimitMan:true,
                'projectInfo.ageRange':'不限制-不限制'
            },()=>{
                if(this.data.noLimitMan === true && this.data.noLimitGirl === true ){
                    this.setData({
                        noLimit:true,
                        noLimitMan:false,
                        noLimitGirl:false
                    })
                }else{
                    this.setData({
                        noLimit:false,
                    })
                }
            })
        }else{
            this.setData({
                noLimitMan:false
            },()=>{
                if(this.data.noLimitMan === true && this.data.noLimitGirl === true ){
                    this.setData({
                        noLimit:true,
                        noLimitMan:false,
                        noLimitGirl:false
                    })
                }else{
                    this.setData({
                        noLimit:false,
                    })
                }
            })
        }
        console.log(this.data.noLimit,'noLimit这个值是什么')
        this.setData({
            'projectInfo.ageRangeMan':ageArr[0][value[0]] + '-' + ageArr[1][value[1]],
        })
    },
    onChoiceGirlAge(event){
        const { value } = event.detail
        if(value[0] !== 0 && value[1] !== 0){
            if( value[0] > value[1] ){
                app.toast('女性起始年龄不可大于结束年龄')
                return
            }
            if( value[1] < value[0] ){
                app.toast('女性结束年龄不可小于开始年龄')
                return
            }
        }
        const { ageGirlArr } = this.data
        if(event.detail.value[0] == 0 && event.detail.value[1] == 0){
            this.setData({
                noLimitGirl:true,
                'projectInfo.ageRange':'不限制-不限制'
            },()=>{
                if(this.data.noLimitMan === true && this.data.noLimitGirl === true ){
                    this.setData({
                        noLimit:true,
                        noLimitMan:false,
                        noLimitGirl:false
                    })
                }else{
                    this.setData({
                        noLimit:false,
                    })
                }
            })
        }else{
            this.setData({
                noLimitGirl:false
            },()=>{
                if(this.data.noLimitMan === true && this.data.noLimitGirl === true ){
                    this.setData({
                        noLimit:true,
                        noLimitMan:false,
                        noLimitGirl:false
                    })
                }else{
                    this.setData({
                        noLimit:false,
                    })
                }
            })
        }
        console.log(this.data.noLimit,'noLimit这个值是什么')
        this.setData({
            'projectInfo.ageRangeWomen':ageGirlArr[0][value[0]] + '-' +ageGirlArr[1][value[1]]
        })
    },
    // 去没名单整理页面
    goSelectList () {
        console.log('去名单整理')
        //  url: '/packageActivity/pages/success/success',
        wx.navigateTo({
          url: '/packagePay/pages/new-name-sorting/new-name-sorting',
        })
    },
    // 编辑项目信息
    onUpdatePayInfo () {
        console.log(this.data, 'datadata')
        // if(){

        // }
        this.updatePayEnterpriseInfo()
    },
    // 项目信息编辑接口
    async updatePayEnterpriseInfo () {
        // this.data.ageSection.replace(/不限制/g,'~')
        const obj = {}
        obj.id = this.data.projectId
        obj.signTimeStart = this.data.projectInfo.signTimeStart === '全天' ? '00:00' : this.data.projectInfo.signTimeStart
        obj.signTimeEnd = this.data.projectInfo.signTimeEnd === '全天' ? '00:00' : this.data.projectInfo.signTimeEnd
        obj.signRange = this.data.address.radius
        obj.signPlace = this.data.describes
        obj.signLocation = this.data.address.longitude + ',' + this.data.address.latitude
        obj.dayNightFlag = this.data.projectInfo.dayNightFlag
        obj.staffUnderpay = this.data.projectInfo.staffUnderpay
        obj.ageRange = this.data.projectInfo.ageRangeMan.replace(/不限制/g,'~')
        obj.ageRangeFemale = this.data.projectInfo.ageRangeWomen.replace(/不限制/g,'~')
        obj.ibeaconIds = this.data.ibeaconIds
        console.log(obj,'obj')
        // 调用项目编辑接口
        await updatePayEnterpriseInfo(obj)
        setTimeout(()=>{
            this.getProjectInfo()
            app.toast('修改成功')
        })
    },
    // 获取名单人员列表
    async getWorkerListGroup () {
        const result = await getWorkerListGroup ({
            projectId: this.data.projectId,
            searchText: '',
            pageSize:9,
            pageNum:1
        })
        this.setData({
            workerList:result.list
        })
        console.log(result, 'result')
    },
    // 管理员列表
    async getProjectLeaderList () {
        const result = await getProjectLeaderList({
            projectId: this.data.projectId
        })
        this.setData({
            leaderList: result
        })
        console.log(this.data.leaderList)
    },
    // 前往硬件打卡地点
    onSelectHardware() {
        wx.navigateTo({
            url: '/packagePay/pages/device-manage/device-manage?isSelect=1',
            events: {
                // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                acceptDataFromOpenedPage:(data)=> {
                    console.log(data, '返回的data数据')
                    this.data.hardwareId = data.data.map(item => {
                        return item.id
                    })
                    this.setData({
                        isDeviceClock: this.data.hardwareId.length ? '已开启硬件打卡' : '',
                        ibeaconIds: this.data.hardwareId
                    })
                }
            },
            success: (res)=> {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', {
                    data: this.data.ibeaconIds
                })
            }
        })
    },
    // 去名单整理
    async onGoListSorting () {
        wx.navigateTo({
            url: '/packagePay/pages/new-name-sorting/new-name-sorting?id=' + this.data.projectId
        })
    },
    // 去团队成员页面
    onGoScreenTeam () {
        wx.navigateTo({
          url: '/packagePay/pages/team-members/team-members?id=' + this.data.projectId,
        })
    },
    // 邀请人员管理项目
    onShareAppMessage() {
        return {
            title: user.userName + '邀请您加入「' + (this.data.projectName || user.companyName) + '」' + (this.data.projectName ? '名单管理' : '项目'),
            path: '/pages/index/index?scene=' + encodeURIComponent('uId=' + user.userId + '&cId=' + user.companyId + (this.data.projectId ?
                ('&pId=' + this.data.projectId) : '')),
            imageUrl: '../../assets/img/resident/share.png'
        }
    },
})